package com.wyw.leetcode.learning.simple;

import java.util.Arrays;

/**
 * leetcode topic 557
 * 反转字符串中的单词
 * @Author Mr Wu （yewen.wu.china@gmail.com）
 * @Date 2021/11/8 16:55
 */
public class Topic557 {
    public static void main(String[] args) {
        String a = "Let's take LeetCode contest";
        System.out.println(reverseWords(a));
    }

    public static String reverseWords(String s) {
        String result = "";
        String[] s1 = s.split(" ");
        for (int i = 0; i< s1.length; i++) {
            result  = result + reverseString(s1[i].toCharArray()) + " ";
        }
        return result.substring(0, s.length());
    }

    public static String reverseWords1(String s) {
        return String.join(" ", Arrays.stream(s.split(" ")).map(x -> new StringBuilder(x).reverse().toString()).toArray(CharSequence[]::new));
    }

    public static String reverseString(char[] s) {
        int i=0;
        int j = s.length -1;
        while(i < j) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
        return String.valueOf(s);
    }
}
